package com.cskaoyan.controller;

import com.cskaoyan.bean.Result;
import com.cskaoyan.bean.User;
import com.cskaoyan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-16
 */
@RequestMapping("user")
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("login")
    // localhost:8080/user/login?username=admin4&password=123456&telNum=13630600788&addr=m78
    public Result<?> login(User user) {
        if (userService.login(user)) return Result.ok("login succeed");
        return Result.error("login failed");
    }

    @RequestMapping("getUsers")
    // localhost:8080/user/getUsers
    public Result<?> getUsers() {
        List<User> userList = userService.getUsers();
        return Result.ok(userList);
    }

    @GetMapping("delete")
    public Result deleteUser(Integer id) {
        if (id <= 0) return Result.error("id不能小于等于0");
        int code = userService.deleteUser(id);
        if (code == 1) {
            return Result.ok();
        }
        return Result.error("删除失败");
    }

    @GetMapping("getUserById")
    public Result getUserById(Integer id) {
        if (id <= 0) return Result.error("id不能小于等于0");
        int code = userService.getUserById(id);
        if (code == 1) {
            return Result.ok();
        }
        return Result.error("查询失败");
    }

    @RequestMapping("update")
    public Result update(User user) {
        int count = userService.update(user);
        if (count == 1) {
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }
    @RequestMapping("updateUserNameById")
    public Result updateUserNameById(Integer id, String name){
      Integer affectedNum =   userService.updateUserNameById(id,name);
      if (affectedNum==1) return  Result.ok("Successfully Updated!");
      return  Result.error("Something Went Wrong!");
    }
}
